package com.atguigu.gmall.payment.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.atguigu.gmall.product.client.model.enums.PaymentStatus;
import com.atguigu.gmall.product.client.model.enums.PaymentType;
import com.atguigu.gmall.product.client.model.order.OrderInfo;
import com.atguigu.gmall.product.client.model.payment.PaymentInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private RabbitService rabbitService;

    //1. 保存支付信息表
    @Override
    public PaymentInfo insert(Long orderId, PaymentType paymentType) {
        //幂等性问题  先点击的支付宝结账  然后在点击的微信结账  导致的
        //1. 查询当前订单是否已经产生了支付信息
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                                            .eq("order_id", orderId));
        if (paymentInfo != null){
            //2. 查询修改就可以了
            //支付类型
            paymentInfo.setPaymentType(paymentType.name());
            //支付时间
            paymentInfo.setCreateTime(new Date());
            paymentInfoMapper.updateById(paymentInfo);
        }else{
            //3. 添加
            paymentInfo = new PaymentInfo();
            //根据订单id查询订单信息
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
            //交易订单号
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            //总金额
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            //订单ID
            paymentInfo.setOrderId(orderInfo.getId());
            //支付类型
            paymentInfo.setPaymentType(paymentType.name());
            //为什么要支付这笔钱   标识体
            paymentInfo.setSubject(orderInfo.getTradeBody());
            //支付时间
            paymentInfo.setCreateTime(new Date());
            //支付状态
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());

            //保存
            paymentInfoMapper.insert(paymentInfo);
        }

        return paymentInfo;
    }

    // 更新支付信息表  银行流水 时间 内容
    @Override
    public void paySuccess(Map<String, String> paramMap) {

        String out_trade_no = paramMap.get("out_trade_no");
        //幂等性问题
        //获取支付信息表中数据 判断是否仍然是支付中
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no", out_trade_no));
        if (paymentInfo != null){   //判断paymentInfo是不是为空
                                    //如果不为空的话  看下支付状态是不是支付中
            if (PaymentStatus.UNPAID.name().equals(paymentInfo.getPaymentStatus())){
                //支付中 改成已经支付
                paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
                //银行流水
                String trade_no = paramMap.get("trade_no");
                paymentInfo.setTradeNo(trade_no);
                //回调时间
                String notify_time = paramMap.get("notify_time");
                try {
                    paymentInfo.setCallbackTime(new SimpleDateFormat
                            ("yyyy-MM-dd HH:mm:ss").parse(notify_time));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //回调内容
                paymentInfo.setCallbackContent(JSONObject.toJSONString(paramMap));

                //更新
                paymentInfoMapper.updateById(paymentInfo);

                //todo  发消息  更新订单  库存
                rabbitService.sendMessage(
                        MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,  //订单支付交换机
                        MqConst.ROUTING_PAYMENT_PAY,   //订单支付routingKey
                        paymentInfo.getOrderId());   //订单Id
            }
        }

    }
}
